home *** CD-ROM | disk | FTP | other *** search
/ The Complete Utilities To…ka 501 Killer Utilities! / 501 Killer Utilities! (Macworld July 1995).cdr / Programming / OutOfPhase1.1 Source / OutOfPhase Folder / FilterFirstOrderHighpass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-02  |  3.7 KB  |  124 lines  |  [TEXT/KAHL]

  1. /* FilterFirstOrderHighpass.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. /* Based on material from pages 184-190 of */
  26. /* Dodge, Charles and Jerse, Thomas A. */
  27. /* Computer Music:  Synthesis, Composition, and Performance */
  28. /* Schirmer Books, New York, 1985 */
  29.  
  30. #include "MiscInfo.h"
  31. #include "Audit.h"
  32. #include "Debug.h"
  33. #include "Definitions.h"
  34.  
  35. #include "FilterFirstOrderHighpass.h"
  36. #include "Memory.h"
  37. #include "FloatingPoint.h"
  38.  
  39.  
  40. struct FirstOrderHighpassRec
  41.     {
  42.         /* link */
  43.         FirstOrderHighpassRec*    Next;
  44.  
  45.         /* state variables */
  46.         float                                        Ym1;
  47.  
  48.         /* coefficients */
  49.         float                                        A;
  50.         float                                        B;
  51.     };
  52.  
  53.  
  54. static FirstOrderHighpassRec*        FreeList = NIL;
  55.  
  56.  
  57. /* flush free list */
  58. void                                            FlushCachedFirstOrderHighpassStuff(void)
  59.     {
  60.         while (FreeList != NIL)
  61.             {
  62.                 FirstOrderHighpassRec*        Temp;
  63.  
  64.                 Temp = FreeList;
  65.                 FreeList = FreeList->Next;
  66.                 ReleasePtr((char*)Temp);
  67.             }
  68.     }
  69.  
  70.  
  71. /* create a new filter record */
  72. FirstOrderHighpassRec*        NewFirstOrderHighpass(void)
  73.     {
  74.         FirstOrderHighpassRec*    Filter;
  75.  
  76.         if (FreeList != NIL)
  77.             {
  78.                 Filter = FreeList;
  79.                 FreeList = FreeList->Next;
  80.             }
  81.          else
  82.             {
  83.                 Filter = (FirstOrderHighpassRec*)AllocPtrCanFail(sizeof(FirstOrderHighpassRec),
  84.                     "FirstOrderHighpassRec");
  85.                 if (Filter == NIL)
  86.                     {
  87.                         return NIL;
  88.                     }
  89.             }
  90.         Filter->Ym1 = 0;
  91.         return Filter;
  92.     }
  93.  
  94.  
  95. /* dispose filter record */
  96. void                                            DisposeFirstOrderHighpass(FirstOrderHighpassRec* Filter)
  97.     {
  98.         CheckPtrExistence(Filter);
  99.         Filter->Next = FreeList;
  100.         FreeList = Filter;
  101.     }
  102.  
  103.  
  104. /* adjust filter coefficients */
  105. void                                            SetFirstOrderHighpassCoefficients(FirstOrderHighpassRec* Filter,
  106.                                                         float Cutoff, long SamplingRate)
  107.     {
  108.         float                                        C;
  109.  
  110.         CheckPtrExistence(Filter);
  111.         C = 2 - DCOS(6.28318530717958648 * Cutoff / SamplingRate);
  112.         Filter->B = C - DSQRT(C * C - 1);
  113.         Filter->A = 1 - Filter->B;
  114.     }
  115.  
  116.  
  117. /* apply filter to a sample value */
  118. float                                            ApplyFirstOrderHighpass(FirstOrderHighpassRec* Filter, float Xin)
  119.     {
  120.         CheckPtrExistence(Filter);
  121.         Filter->Ym1 = Filter->A * Xin - Filter->B * Filter->Ym1;
  122.         return Filter->Ym1;
  123.     }
  124.